<?php

namespace App\Api\Controllers;

use App\Models\Crousesheet;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use Encore\Admin\Facades\Admin;

class GetTutorCrousesController extends Controller
{
    public function get_tutor_crouses(Request $request)
    {

        $user = $request->get('user');
        $DateId = $request->get('q');
        switch ($DateId) {
            case '1':
                $date = date('Y-m-d',strtotime("-7 day"));
                break;
            case '2':
                $date = date('Y-m-d',strtotime("-6 day"));
                break;
            case '3':
                $date = date('Y-m-d',strtotime("-5 day"));
                break;
            case '4':
                $date = date('Y-m-d',strtotime("-4 day"));
                break;
            case '5':
                $date = date('Y-m-d',strtotime("-3 day"));
                break;
            case '6':
                $date = date('Y-m-d',strtotime("-2 day"));
                break;
            case '7':
                $date = date('Y-m-d',strtotime("-1 day"));
                break;
            case '8':
                $date = date('Y-m-d',time());
                break;
            case '9':
                $date = date('Y-m-d',strtotime("+1 day"));
                break;
            case '10':
                $date = date('Y-m-d',strtotime("+2 day"));
                break;
            case '11':
                $date = date('Y-m-d',strtotime("+3 day"));
                break;
            case '12':
                $date = date('Y-m-d',strtotime("+4 day"));
                break;
            case '13':
                $date = date('Y-m-d',strtotime("+5 day"));
                break;
            case '14':
                $date = date('Y-m-d',strtotime("+6 day"));
                break;
            case '15':
                $date = date('Y-m-d',strtotime("+7 day"));
                break;
            default:
                # code...
                break;
        }
        $section = date('w',strtotime($date));
        if ($section=='0') {
            $section = '7';
        }
        //dd($section);
        //$list = Crousesheet::where('section', $section)->where('tutor_id',$user)->get(['id',DB::raw('select ')]);
        $list = DB::table('crousesheets')         //将两张表拼接起来
            // ->leftjoin('crouse_times', 'crousesheets.period', '=', 'crouse_times.id')
            ->leftjoin('crouses','crousesheets.crouse_id', '=', 'crouses.id')
            ->leftjoin('squads','crousesheets.squads_id', '=', 'squads.id')
            ->leftjoin('classrooms','crousesheets.classroom_id','=','classrooms.id')
            ->where('crousesheets.section',$section)->where('crousesheets.status','=',1)
            ->where('crousesheets.tutor_id',$user)
            ->select('crousesheets.id',DB::raw('concat("【班级：",squads.name,"】【教室：",classrooms.name,"】【",
            CASE when crousesheets.section = 1 then "周一" when  crousesheets.section = 2 then "周二" when crousesheets.section = 3 then "周三"
                when crousesheets.section = 4 then "周四"
                when crousesheets.section = 5 then "周五"
                when crousesheets.section = 6 then "周六"
                when crousesheets.section = 7 then "周日"
            END
                ,"第",crousesheets.period,"节课","(",crousesheets.start_time,"-",crousesheets.end_time,")】【课程：",crouses.name,"】") as text'))->get();
        $arr = array('0'=>'请选择课程');
        foreach ($list as $key => $value) {
            $arr[] = array("id" => $value->id, "text" => $value->text);
        }
        return $arr;//返回数组到分组的option
    }
}
